package pri.syl.xiaowu.websocket.init;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import pri.syl.xiaowu.websocket.autoconfigure.WebsocketProperties;

import javax.annotation.Resource;

/**
 *
 *
 * @description
 * @author YangXinFu
 * @date 17:14 2022/7/20
 **/
@Configuration
public class NacosRegisterInit implements CommandLineRunner {
    @Resource
    NacosDiscoveryProperties nacosDiscoveryProperties;
    @Resource
    private WebsocketProperties websocketProperties;

    @Override
    public void run(String... args) throws Exception {
        try {
            nacosDiscoveryProperties.namingServiceInstance().registerInstance(websocketProperties.getName(), nacosDiscoveryProperties.getIp(),
                    websocketProperties.getPortOrDefault());
        } catch (NacosException e) {
            throw new RuntimeException("Nacos 注册失败。");
        }

    }
}
